package com.leinao.images.controller;

import com.github.pagehelper.PageInfo;
import com.leinao.commons.Response;
import com.leinao.constant.Constant;
import com.leinao.constant.ImageErrorEnum;
import com.leinao.images.dto.*;
import com.leinao.images.service.ImagesService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSON;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 镜像处理控制器
 *
 * @author  suqiliang
 * @date 2019年04月18日 下午2:47:18
 *
 */
@RestController
@CrossOrigin
public class ImagesController {
	
	private Logger logger = LoggerFactory.getLogger(ImagesController.class);
	
	@Autowired
	private ImagesService imagesService;
	
	@ApiOperation(value="获取镜像", notes="返回镜像列表")
    @GetMapping(value = "/images/getList")
	public Response<List<ImagesDto>> getImagesList(@ApiParam(value = "编程语言", required = true) @RequestParam String language,
												   @ApiParam(value = "框架", required = true) @RequestParam String framework,
												   HttpServletRequest request){
		logger.info("getImagesList param language: {}, framework: {}", language, framework);

		String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION);
		LoginResultDto userInfo = JSON.parseObject(obj, LoginResultDto.class);

		// 获取镜像列表
		return imagesService.getImagesList(language, framework, userInfo.getUserId());
	}

	@ApiOperation(value="获取编程语言", notes="返回编程语言列表")
	@GetMapping(value = "/language/getList")
	public Response<List<LanguageDto>> getLanguageList(){

		// 获取编程语言列表
		return imagesService.getLanguageList();
	}

	@ApiOperation(value="获取框架", notes="返回框架列表")
	@GetMapping(value = "/framework/getList")
	public Response<List<FrameworkDto>> getFrameworkList(){

		// 获取框架列表
		return imagesService.getFrameworkList();
	}

	/**
	 *
	 * @Description: 创建自定义镜像
	 * @param file : 上传dockerfile文件
	 * @param name : 名称
	 * @param content : 镜像简介
	 * @param contentMk : 镜像mk简介
	 * @param languageId : 算法ID
	 * @param frameworkId : 框架ID
	 * @param request :
	 * @return: com.leinao.commons.Response<java.lang.Void>
	 * @Author: 曹阳
	 * @date: 2019/5/15 15:20
	 *
	 **/
	@ApiOperation(value="创建自定义镜像", notes="创建自定义镜像")
	@PostMapping(value = "/images/createUserImage")
	public Response<Void> createUserImage(@ApiParam(value = "上传dockerfile文件", required = true) MultipartFile file, @ApiParam(value = "名称", required = true) String name,
										  @ApiParam(value = "镜像简介", required = true) String content, @ApiParam(value = "镜像mk简介", required = true) String contentMk,
										  @ApiParam(value = "算法ID", required = true) Long languageId, @ApiParam(value = "框架ID", required = true) Long frameworkId, HttpServletRequest request){
		if (StringUtils.isBlank(name) || StringUtils.isBlank(content) || StringUtils.isBlank(contentMk)) {
			return Response.error(Constant.RESPONSE_STATUS_SUCCESS, ImageErrorEnum.PARAMETER_NULL.getSeq(), ImageErrorEnum.PARAMETER_NULL.getDesc(),
					null);
		}
		if (languageId == null || frameworkId == null || file == null || file.isEmpty()) {
			return Response.error(Constant.RESPONSE_STATUS_SUCCESS, ImageErrorEnum.PARAMETER_NULL.getSeq(), ImageErrorEnum.PARAMETER_NULL.getDesc(),
					null);
		}
		String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION);
		LoginResultDto userInfo = JSON.parseObject(obj, LoginResultDto.class);
		Integer type = 3;
		return imagesService.createUserImage(file, name.toLowerCase(), type, userInfo.getUserId(), content, contentMk, languageId, frameworkId);
	}

	/**
	 *
	 * @Description: 通过用户ID查询用户自定义镜像
	 * @param pageNum : 当前页
	 * @param pageSize : 每页条数
	 * @param request : 
	 * @return: com.leinao.commons.Response<java.util.List<com.leinao.images.dto.ImagesDto>>
	 * @Author: 曹阳
	 * @date: 2019/5/15 16:29
	 *
	 **/
	@ApiOperation(value="查询自定义镜像列表", notes="查询自定义镜像列表")
	@PostMapping(value = "/images/getUserImagesList")
	public Response<PageInfo> getUserImagesList(@ApiParam(value = "当前页")Integer pageNum, @ApiParam(value = "每页条数")Integer pageSize, HttpServletRequest request){
		pageNum = null == pageNum ? Constant.DEFAULT_PAGE_NUMBER : pageNum;
		pageSize = null == pageSize ? Constant.DEFAULT_PAGE_SIZE : pageSize;
		String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION);
		LoginResultDto userInfo = JSON.parseObject(obj, LoginResultDto.class);
		return imagesService.getUserImagesList(userInfo.getUserId(), pageNum, pageSize);
	}

	/**
	 *
	 * @Description: 通过镜像ID查询用户自定义镜像
	 * @param id : 镜像ID
	 * @param request :
	 * @return: com.leinao.commons.Response<com.leinao.images.dto.ImageInfo>
	 * @Author: 曹阳
	 * @date: 2019/5/15 17:14
	 *
	 **/
	@ApiOperation(value="通过镜像ID查询用户自定义镜像", notes="通过镜像ID查询用户自定义镜像")
	@PostMapping(value = "/images/getUserImageById")
	public Response<ImageInfo> getUserImageById(@ApiParam(value = "镜像ID", required = true) Long id, HttpServletRequest request){
		if (id == null) {
			return Response.error(Constant.RESPONSE_STATUS_SUCCESS, ImageErrorEnum.PARAMETER_NULL.getSeq(), ImageErrorEnum.PARAMETER_NULL.getDesc(),
					null);
		}
		String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION);
		LoginResultDto userInfo = JSON.parseObject(obj, LoginResultDto.class);
		return imagesService.getUserImageById(id, userInfo.getUserId());
	}

	/**
	 *
	 * @Description:  更新镜像简介
	 * @param id : 镜像ID
	 * @param content : 镜像简介
	 * @param contentMk : 镜像mk简介
	 * @param request :
	 * @return: com.leinao.commons.Response<com.leinao.images.dto.ImageInfo>
	 * @Author: 曹阳
	 * @date: 2019/5/15 17:29
	 *
	 **/
	@ApiOperation(value="更新镜像简介", notes="更新镜像简介")
	@PostMapping(value = "/images/updateUserImageContent")
	public Response<Void> updateUserImageContent(@ApiParam(value = "镜像ID", required = true) Long id, @ApiParam(value = "镜像简介", required = true) String content, @ApiParam(value = "镜像mk简介", required = true) String contentMk, HttpServletRequest request){
		if (id == null) {
			return Response.error(Constant.RESPONSE_STATUS_SUCCESS, ImageErrorEnum.PARAMETER_NULL.getSeq(), ImageErrorEnum.PARAMETER_NULL.getDesc(),
					null);
		}
		if (StringUtils.isBlank(content) || StringUtils.isBlank(contentMk)) {
			return Response.error(Constant.RESPONSE_STATUS_SUCCESS, ImageErrorEnum.PARAMETER_NULL.getSeq(), ImageErrorEnum.PARAMETER_NULL.getDesc(),
					null);
		}
		String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION);
		LoginResultDto userInfo = JSON.parseObject(obj, LoginResultDto.class);
		return imagesService.updateUserImageContent(id, content, contentMk, userInfo.getUserId());
	}

	/**
	 *
	 * @Description: 删除镜像
	 * @param id : 镜像ID
	 * @param request :
	 * @return: com.leinao.commons.Response<java.lang.Void>
	 * @Author: 曹阳
	 * @date: 2019/5/17 10:10
	 *
	 **/
	@ApiOperation(value="删除镜像", notes="删除镜像")
	@PostMapping(value = "/images/deleteUserImage")
	public Response<Void> deleteUserImage(@ApiParam(value = "镜像ID", required = true) Long id, HttpServletRequest request){
		if (id == null) {
			return Response.error(Constant.RESPONSE_STATUS_SUCCESS, ImageErrorEnum.PARAMETER_NULL.getSeq(), ImageErrorEnum.PARAMETER_NULL.getDesc(),
					null);
		}
		String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION);
		LoginResultDto userInfo = JSON.parseObject(obj, LoginResultDto.class);
		return imagesService.deleteUserImage(id, userInfo.getUserId());
	}
}
